import Types from '../../action/types'

const defaultState = {}

/**
 * popular:{
 *   java:{
 *     items:[],
 *     isLoading:false
 *   },
 *   ios:{
 *     items:[],
 *     isLoading:false
 *   }
 * }
 *
 * 难点:如何动态的设置store和动态获取store（store key不固定）
 * @param state
 * @param action
 * @return {{theme: *}}
 */
export default function onAction(state = defaultState, action) {
	switch (action.type) {
		//下拉刷新成功
		case Types.TRENDING_REFRESH_SUCCESS:
			return {
				...state,
				[action.storeName]: {
					...state[action.storeName],
					items: action.items, //原始数据
					projectModels: action.projectModels, //此次要展示的数据
					isLoading: false,
					hideLoadingMore: false,
					pageIndex: action.pageIndex
				}
			}
		//下拉刷新失败
		case Types.TRENDING_REFRESH_FAIL:
			return {
				...state,
				[action.storeName]: {
					...state[action.storeName],
					isLoading: false
				}
			}
		//下拉刷新
		case Types.TRENDING_REFRESH:
			return {
				...state,
				[action.storeName]: {
					...state[action.storeName],
					isLoading: true,
					hideLoadingMore: true
				}
			}
		//上拉加载更多成功
		case Types.TRENDING_LOAD_MORE_SUCCESS:
			return {
				...state,
				[action.storeName]: {
					...state[action.storeName],
					projectModels: action.projectModels,
					hideLoadingMore: false,
					pageIndex: action.pageIndex
				}
			}
		//上拉加载更多失败
		case Types.TRENDING_LOAD_MORE_FAIL:
			return {
				...state,
				[action.storeName]: {
					...state[action.storeName],
					hideLoadingMore: true,
					pageIndex: action.pageIndex
				}
			}
		default:
			return state
	}
}
